package com.zyzd.general.module.recyclerview.slidebar;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.zyzd.general.R;
import com.zyzd.general.entity.local.UserEntity;
import com.zyzd.general.widget.MiddleSplitView;
import com.zyzd.retrofit.base.BaseAdapter;

import java.util.ArrayList;

/**
 * Created by lzy on 2018/5/16 0016.
 * description ${快速索引案例 Adapter}
 */
class SidebarAdapter extends BaseAdapter<SidebarAdapter.SideBarViewHolder, UserEntity> implements SectionIndexer {

    SidebarAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public SideBarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SideBarViewHolder(mInflater.inflate(R.layout.item_sidebar, parent, false));
    }

    @Override
    public void onBindHolder(@NonNull SideBarViewHolder holder, int position) {
        holder.tvContent.setText(getItem(position).getUserName());

        if (position < (getItemCount() - 1) && !getItem(position).getInitialLetter().equals(getItem(position + 1).getInitialLetter())) {
            holder.middleSplitView.setVisibility(View.VISIBLE);
            holder.middleSplitViewOfStick.setVisibility(View.VISIBLE);
            holder.middleSplitViewOfMark.setVisibility(View.VISIBLE);
            holder.middleSplitViewOfDelete.setVisibility(View.VISIBLE);
            holder.viewSplit.setVisibility(View.GONE);
            holder.viewSplitOfStick.setVisibility(View.GONE);
            holder.viewSplitOfMark.setVisibility(View.GONE);
            holder.viewSplitOfDelete.setVisibility(View.GONE);
        } else {
            holder.middleSplitView.setVisibility(View.GONE);
            holder.middleSplitViewOfStick.setVisibility(View.GONE);
            holder.middleSplitViewOfMark.setVisibility(View.GONE);
            holder.middleSplitViewOfDelete.setVisibility(View.GONE);
            holder.viewSplit.setVisibility(View.VISIBLE);
            holder.viewSplitOfStick.setVisibility(View.VISIBLE);
            holder.viewSplitOfMark.setVisibility(View.VISIBLE);
            holder.viewSplitOfDelete.setVisibility(View.VISIBLE);
        }

    }

    private SparseIntArray positionOfSection;
    private SparseIntArray sectionOfPosition;
    private ArrayList<String> list;

    @Override
    public Object[] getSections() {
        positionOfSection = new SparseIntArray();
        sectionOfPosition = new SparseIntArray();
        int count = getItemCount();
        list = new ArrayList<String>();
        list.add(mContext.getString(R.string.common_search_header));
        positionOfSection.put(0, 0);
        sectionOfPosition.put(0, 0);
        for (int i = 1; i < count; i++) {
            String letter = getItem(i).getInitialLetter();
            int section = list.size() - 1;
            if (list.get(section) != null && !list.get(section).equals(letter)) {
                list.add(letter);
                section++;
                positionOfSection.put(section, i);
            }
            sectionOfPosition.put(i, section);
        }
        return list.toArray(new String[list.size()]);
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return positionOfSection.get(sectionIndex);
    }

    @Override
    public int getSectionForPosition(int position) {
        return sectionOfPosition.get(position);
    }

    static class SideBarViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvContent;
        private final MiddleSplitView middleSplitView;
        private final MiddleSplitView middleSplitViewOfStick;
        private final MiddleSplitView middleSplitViewOfMark;
        private final MiddleSplitView middleSplitViewOfDelete;
        private final View viewSplit;
        private final View viewSplitOfStick;
        private final View viewSplitOfMark;
        private final View viewSplitOfDelete;

        SideBarViewHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            middleSplitView = itemView.findViewById(R.id.middleSplitView);
            middleSplitViewOfStick = itemView.findViewById(R.id.middleSplitView_of_stick);
            middleSplitViewOfMark = itemView.findViewById(R.id.middleSplitView_of_mark);
            middleSplitViewOfDelete = itemView.findViewById(R.id.middleSplitView_of_delete);
            viewSplit = itemView.findViewById(R.id.view_split);
            viewSplitOfStick = itemView.findViewById(R.id.view_split_of_stick);
            viewSplitOfMark = itemView.findViewById(R.id.view_split_of_mark);
            viewSplitOfDelete = itemView.findViewById(R.id.view_split_of_delete);
        }
    }
}
